package cn.kingyang.yiyi.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;

import butterknife.BindView;
import cn.kingyang.yiyi.R;
import cn.kingyang.yiyi.app.App;
import cn.kingyang.yiyi.mvp.presenter.MainPresenter;
import cn.kingyang.yiyi.mvp.view.MainView;
import cn.kingyang.yiyi.ui.fragment.ForumFragment;
import cn.kingyang.yiyi.ui.fragment.HomeFragment;
import cn.kingyang.yiyi.ui.fragment.MyFragment;
import cn.kingyang.yiyi.ui.fragment.NobleFragment;
import cn.kingyang.yiyi.ui.view.BottomBar;
import cn.kingyang.yiyi.ui.view.BottomBarTab;
import cn.kingyang.yiyi.utils.AppUtil;
import me.yokeyword.fragmentation.SupportFragment;
import me.yokeyword.fragmentation.anim.FragmentAnimator;
import me.yokeyword.fragmentation.helper.FragmentLifecycleCallbacks;

public class MainActivity extends BaseActivity<MainView, MainPresenter> implements MainView{

    private static final int REQ_MSG = 10;
    public static final int HOME = 0;
    public static final int NOBLE = 1;
    public static final int FORUM = 2;
    public static final int IMMORTAL =3;
    @BindView(R.id.bottomBar)
    BottomBar bottomBar;

    private SupportFragment[] mFragments = new SupportFragment[4];

    @Override
    protected MainPresenter getPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        ((HomeFragment)mFragments[HOME]).update();
        BottomBarTab noble= bottomBar.getItem(1);
        BottomBarTab home= bottomBar.getItem(0);
        noble.update(App.icon[App.level+1],App.levels.get(App.level+1)+"区");
        home.update(App.icon[App.level],App.levels.get(App.level)+"区");
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        sousuo.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                start(SearchActivity.class);
//            }
//        });
        if (savedInstanceState == null) {
            loadRootFragment(R.id.fl_tab_container, HomeFragment.newInstance());
        }
        // 可以监听该Activity下的所有Fragment的18个 生命周期方法
        registerFragmentLifecycleCallbacks(new FragmentLifecycleCallbacks() {

            @Override
            public void onFragmentSupportVisible(SupportFragment fragment) {
                Log.i("MainActivity", "onFragmentSupportVisible--->" + fragment.getClass().getSimpleName());
            }
        });
//        if (savedInstanceState == null) {
            mFragments[HOME] = HomeFragment.newInstance();
            mFragments[NOBLE] = NobleFragment.newInstance();
            mFragments[FORUM] = ForumFragment.newInstance();
            mFragments[IMMORTAL] = MyFragment.newInstance();
            loadMultipleRootFragment(R.id.fl_tab_container, HOME,
                    mFragments[HOME],
                    mFragments[NOBLE],
                    mFragments[FORUM],
                    mFragments[IMMORTAL]);
//        } else {
//            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
//
//            // 这里我们需要拿到mFragments的引用,也可以通过getChildFragmentManager.getFragments()自行进行判断查找(效率更高些),用下面的方法查找更方便些
//            mFragments[HOME] = findChildFragment(HomeFragment.class);
//            mFragments[CATALOG] = findChildFragment(NobleFragment.class);
//            mFragments[RANK] = findChildFragment(MyFragment.class);
//            mFragments[MY] = findChildFragment(ForumFragment.class);
//        }
        bottomBar
                .addItem(new BottomBarTab(this,App.icon[App.level],App.levels.get(App.level)+"区"))
                .addItem(new BottomBarTab(this,App.icon[App.level+1],App.levels.get(App.level+1)+"区"))
                .addItem(new BottomBarTab(this,new int[]{R.drawable.dv,R.drawable.du}, "私房视频"))
                .addItem(new BottomBarTab(this,new int[]{R.drawable.dv,R.drawable.du}, "论坛"));

        // 模拟未读消息
//        bottomBar.getItem(FIRST).setUnreadCount(9);
        BottomBarTab tab = bottomBar.getItem(NOBLE);
        tab.setUnreadCount(100);
        bottomBar.setOnTabSelectedListener(new BottomBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(mFragments[position], mFragments[prePosition]);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {
                // 这里推荐使用EventBus来实现 -> 解耦
                // 在FirstPagerFragment,FirstHomeFragment中接收, 因为是嵌套的Fragment
                // 主要为了交互: 重选tab 如果列表不在顶部则移动到顶部,如果已经在顶部,则刷新
//                EventBus.getDefault().post(new TabSelectedEvent(position));
            }
        });
//        new Pau().onPayStart(this,false);

    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        // 设置默认Fragment动画  默认竖向(和安卓5.0以上的动画相同)
        return super.onCreateFragmentAnimator();
        // 设置横向(和安卓4.x动画相同)
//        return new DefaultHorizontalAnimator();
        // 设置自定义动画
//        return new FragmentAnimator(enter,exit,popEnter,popExit);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void onError(Throwable t) {

    }
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            AppUtil.showPopupWindow(this,bottomBar,true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
